package cn.yunhe.service;

import java.io.File;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import cn.yunhe.dao.BookDao;
import cn.yunhe.entity.Book;
import cn.yunhe.util.SqlSessionFactoryUtil;

public class BookService {
/*	
	public static void main(String[] args) {
		SqlSession sqlSession=null;
		sqlSession=SqlSessionFactoryUtil.getsqlsession();
		BookDao bookDao=sqlSession.getMapper(BookDao.class);
		
		Book book = new Book("A","A","A",3,3,"A");
		bookDao.addBook(book);
		sqlSession.commit();
		
	}*/
	/**
	 * 根据作者查询
	 * @param author
	 * @return
	 */
	public List<Book> getBookByAuthor(String author) {
		SqlSession sqlSession=null;
		sqlSession=SqlSessionFactoryUtil.getsqlsession();
		BookDao bookDao=sqlSession.getMapper(BookDao.class);
		List<Book> list=bookDao.getBookByAuthor(author);
		return list;
	}
	/**
	 * 根据库存查询
	 * @param 
	 * @return
	 */
	public List<Book> getBookByCount(int count) {
		SqlSession sqlSession=null;
		sqlSession=SqlSessionFactoryUtil.getsqlsession();
		BookDao bookDao=sqlSession.getMapper(BookDao.class);
		List<Book> list=bookDao.getBookByCount(count);
		return list;
	}
	
	
	/**
	 * 获取所有书籍
	 * @return
	 */
	public List<Book> getAllBook() {
		SqlSession sqlSession=null;
		sqlSession=SqlSessionFactoryUtil.getsqlsession();
		BookDao bookDao=sqlSession.getMapper(BookDao.class);
		List<Book> list=bookDao.getAllBook();
		return list;
	}
	
	public Boolean delBook(int bookid) {
		SqlSession sqlSession=null;
		sqlSession=SqlSessionFactoryUtil.getsqlsession();
		BookDao bookDao=sqlSession.getMapper(BookDao.class);
		Boolean flag=bookDao.delBook(bookid);
		sqlSession.commit();
		return flag;
	}
	
	/**
	 * 添加书籍
	 * @param bookname
	 * @param author
	 * @param publish
	 * @param price
	 * @param count
	 * @param picture
	 * @return
	 */
	public Boolean addBook(String bookname,String author,String publish,Double price,int count,String picture) {
		SqlSession sqlSession=null;
		sqlSession=SqlSessionFactoryUtil.getsqlsession();
		BookDao bookDao=sqlSession.getMapper(BookDao.class);
		Boolean flag=bookDao.addBook(bookname, author, publish, price, count, picture);
		sqlSession.commit();
		return flag;
	}

}
